package com.school.aftersalemanagement.controller.ui.bicyclemanagement;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.school.aftersalemanagement.R;
import com.school.aftersalemanagement.controller.adapter.BicycleManagementAdapter;
import com.school.aftersalemanagement.entity.Bike;
import com.school.aftersalemanagement.global.BaseToolBarActivity;
import com.school.aftersalemanagement.service.BicycleService;
import com.school.aftersalemanagement.service.impl.BicycleServiceimpl;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnEditorAction;
import butterknife.OnItemClick;

public class BicycleManagementActivity extends BaseToolBarActivity {
    @BindView(R.id.bicycleManagementList)
    ListView bicycleManagementList;
    @BindView(R.id.searchbynumber_linearLayout)
    LinearLayout searchByNumberLinearLayout;
    @BindView(R.id.searchbynumber_edit)
    EditText searchByNumberEdit;
    private BicycleManagementAdapter bicycleManagementAdapter;
    private BicycleService bicycleService;
    private List<Bike> bikes;
    private int GET_BIKELIST = 1;
    private int GET_FENCE = 2;
    private int pageNumber;
    private String location;
    private List<String> fence_Ids;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == GET_BIKELIST) {
                List<Bike> bike = (List<Bike>)msg.obj;
                bikes.addAll(bike);
                if(bikes.size()>=1) {
                    bicycleManagementAdapter.notifyDataSetChanged();
                }
            }else if (msg.what == GET_FENCE) {
                if(fence_Ids!=null&&!feaceId.isEmpty())
                    fence_Ids.clear();
                fence_Ids = (List<String>)msg.obj;
                fence_Ids.add("无");
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bicyclemanagement);
        setToolbarText("车辆管理");
        bikes = new ArrayList<>();
        bicycleManagementAdapter = new BicycleManagementAdapter(this,bikes);
        location = getIntent().getStringExtra("location");
        bicycleManagementList.setAdapter(bicycleManagementAdapter);
        bicycleService = new BicycleServiceimpl(this,mHandler);
        bicycleService.getBicycleList(GET_BIKELIST,location,pageNumber);
        bicycleManagementList.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount > 0) {
                    pageNumber++;
                    if(T==0)
                        bicycleService.getBicycleList(GET_BIKELIST,location,pageNumber);
                    else if(T==1)
                        bicycleService.searchBike(GET_BIKELIST,"",leaseStatus,feaceId,remark,sortRideTimes,sortAfterTimes,pageNumber+"");
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        T = 0;
        pageNumber = 1;
        bicycleService.getFence(GET_FENCE);
    }

    @OnItemClick(R.id.bicycleManagementList)
    public void onItemClicked(AdapterView<?> parent, View view, int position, long id){
        switch (parent.getId()){
            case R.id.bicycleManagementList:
                Intent resultIntent = new Intent();
                Bike bike = bikes.get(position);
                resultIntent.putExtra("bikeNum",bike.getNumber());
                setResult(RESULT_OK, resultIntent);
                finish();
                break;
        }
    }

    private int T;
    private String leaseStatus = null;//0未租借1已租借
    private String feaceId = "";//
    private String remark = null;//0未备注1已备注
    private String sortRideTimes = "";//1正序2倒序
    private String sortAfterTimes = "";//1正序2倒序
    @BindView(R.id.searchbyleasestatus)
    TextView searchbyleasestatus;
    @BindView(R.id.searchbyfeace)
    TextView searchbyfeace;
    @BindView(R.id.searchbyremark)
    TextView searchbyremark;
    @BindView(R.id.searchbysortridetime)
    TextView searchbysortridetime;
    @BindView(R.id.searchbysortaftertime)
    TextView searchbysortaftertime;
    @OnClick({R.id.searchbynumber,R.id.searchbynumber_cancel,R.id.searchbyleasestatus,R.id.searchbyfeace,R.id.searchbyremark,R.id.searchbysortridetime,R.id.searchbysortaftertime,R.id.search})
    public void onViewClicked(View view){

        switch (view.getId()){
            case R.id.searchbynumber:
                searchByNumberLinearLayout.setVisibility(View.VISIBLE);
                break;
            case R.id.searchbynumber_cancel:
                searchByNumberLinearLayout.setVisibility(View.GONE);
                break;
            case R.id.searchbyleasestatus:
                final String[] leasestatus = new String[]{"未租借","已租借","无"};
                new AlertDialog.Builder(this)//对话框标题
                        .setItems(leasestatus,new DialogInterface.OnClickListener() {
                            @Override//取消按钮
                            public void onClick(DialogInterface dialog, int which) {
                                String str = leasestatus[which];
                                if(which==2)
                                    str = "租借";
                                searchbyleasestatus.setText(str);
                                switch(which){
                                    case 0:
                                        leaseStatus = "0";
                                        break;
                                    case 1:
                                        leaseStatus = "1";
                                        break;
                                    case 2:
                                        leaseStatus = null;
                                        break;
                                }
                            }
                        })//对话框提示正文
                        .setCancelable(false)//点击其他区域关闭对话框
                        .show();
                break;
            case R.id.searchbyfeace:
                final String[] feace = fence_Ids.toArray(new String[fence_Ids.size()]);
                new AlertDialog.Builder(this)//对话框标题
                        .setItems(feace,new DialogInterface.OnClickListener() {
                            @Override//取消按钮
                            public void onClick(DialogInterface dialog, int which) {
                                String str = feace[which];
                                feaceId = feace[which];
                                if("无".equals(feace[which])){
                                    feaceId = "";
                                    str = "围栏";
                                }
                                searchbyfeace.setText(str);

                            }
                        })//对话框提示正文
                        .setCancelable(false)//点击其他区域关闭对话框
                        .show();
                break;
            case R.id.searchbyremark:
                final String[] remarks = new String[]{"未备注","已备注","无"};
                new AlertDialog.Builder(this)//对话框标题
                        .setItems(remarks,new DialogInterface.OnClickListener() {
                            @Override//取消按钮
                            public void onClick(DialogInterface dialog, int which) {
                                String str = remarks[which];
                                if(which==2)
                                    str = "备注";
                                searchbyremark.setText(str);
                                switch(which){
                                    case 0:
                                        remark = "0";
                                        break;
                                    case 1:
                                        remark = "1";
                                        break;
                                    case 2:
                                        remark = null;
                                        break;
                                }
                            }
                        })//对话框提示正文
                        .setCancelable(false)//点击其他区域关闭对话框
                        .show();
                break;
            case R.id.searchbysortridetime:
                final String[] sortridetime = new String[]{"正序","倒叙","无"};
                new AlertDialog.Builder(this)//对话框标题
                        .setItems(sortridetime,new DialogInterface.OnClickListener() {
                            @Override//取消按钮
                            public void onClick(DialogInterface dialog, int which) {
                                String str = sortridetime[which];
                                if(which==2)
                                    str = "总数";
                                searchbysortridetime.setText(str);
                                switch(which){
                                    case 0:
                                        sortRideTimes = "1";
                                        break;
                                    case 1:
                                        sortRideTimes = "2";
                                        break;
                                    case 2:
                                        sortRideTimes = "";
                                        break;
                                }
                            }
                        })//对话框提示正文
                        .setCancelable(false)//点击其他区域关闭对话框
                        .show();
                break;
            case R.id.searchbysortaftertime:
                final String[] sortaftertime = new String[]{"正序","倒叙","无"};
                new AlertDialog.Builder(this)//对话框标题
                        .setItems(sortaftertime,new DialogInterface.OnClickListener() {
                            @Override//取消按钮
                            public void onClick(DialogInterface dialog, int which) {
                                String str = sortaftertime[which];
                                if(which==2)
                                    str = "养后数";
                                searchbysortaftertime.setText(str);
                                switch(which){
                                    case 0:
                                        sortAfterTimes = "1";
                                        break;
                                    case 1:
                                        sortAfterTimes = "2";
                                        break;
                                    case 2:
                                        sortAfterTimes = "";
                                        break;
                                }
                            }
                        })//对话框提示正文
                        .setCancelable(false)//点击其他区域关闭对话框
                        .show();
                break;
            case R.id.search:
                T=1;
                pageNumber=1;
                bikes.clear();
                bicycleManagementAdapter.notifyDataSetChanged();
                bicycleService.searchBike(GET_BIKELIST,"",leaseStatus,feaceId,remark,sortRideTimes,sortAfterTimes,pageNumber+"");
                break;
        }
    }

    @OnEditorAction(R.id.searchbynumber_edit)
    public boolean onEditorAction(TextView textView, int i, KeyEvent event){
        if (i == EditorInfo.IME_ACTION_SEARCH||event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
            switch (event.getAction()) {
                case KeyEvent.ACTION_UP:
                    T=3;
                    bikes.clear();
                    bicycleService.searchBike(GET_BIKELIST,searchByNumberEdit.getText().toString(),"","","","","","");
                    break;
            }
            return true;
        }
        return false;
    }

    @Override
    protected void onPause() {
        super.onPause();
        finish();
    }
}
